<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Cate as CateModel;
use app\common\model\MusicCate as MusicCateModel;
use app\common\model\Music as MusicModel;
use app\common\model\VideoCate as VideoCateModel;
use app\common\model\Video as VideoModel;
use app\common\model\News as NewsModel;
use app\common\model\Classes as ClassesModel;

/**
 * Cate 分类
 * Class Cate
 * @package app\home\controller
 */
class Cate extends Base
{
    /**
     * [getInfo  获取资讯分类下的子分类]
     * @param $type 1资讯文章 2佛经 3佛片 4佛乐 5禅语
     * @return json
     * @author [C。]
     */
    public function getInfo()
    {
        $model = new CateModel;
        $mmodel = new MusicCateModel;
        $vmodel = new VideoCateModel;
        $param = input('param.');
        $type = $param['type'];
        $map = [];
        try{
            if($type == 1 || $type == 7){
                $map['class_id'] = $type;
                $map['states'] = 1;
                $cate = $model->getConditionByFields($map,'id,cname,sort');
            }elseif($type == 2){
                $map['class_id'] = 2;
                $map['states'] = 1;
                $cate = $model->getConditionByFields($map,'id,cname');
            }elseif($type == 3){
                $map['class_id'] = 3;
                $map['states'] = 1;
                $cate = $vmodel->getConditionByFields($map,'id,cname');
            }elseif($type == 4){
                $map['class_id'] = 4;
                $map['states'] = 1;
                $cate = $mmodel->getConditionByFields($map,'id,cname');
            }elseif($type == 5){
                $map['class_id'] = 5;
                $map['states'] = 1;
                $cate = $mmodel->getConditionByFields($map,'id,cname');
            }elseif($type == 6){
                $map['class_id'] = 6;
                $map['states'] = 1;
                $cate = $vmodel->getConditionByFields($map,'id,cname');
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$cate);
    }
    /**
     * [getCateMess  子分类下的内容]
     * @param $type 1资讯文章 2佛经 3佛片 4佛乐 5禅语
     * @return json
     * @author [C。]
     */
    public function getCateMess()
    {
        $model = new NewsModel();
        $mmodel = new MusicModel;
        $vmodel = new VideoModel;
        $param = input('param.');
        $type = $param['type'];
        $this->getPageAndSize($param);
        $map = [];
        if(!empty($param['key'])){
            $map['title'] = ['like', "%" . $param['key'] . "%"];
        }
        try {
            if ($type == 1 || $type == 2) {
                $map['states'] = 1;
                $map['cateid'] = $param['id'];
                $count = $model->getCounts($map);//满足条件的总数
                $allpage = intval(ceil($count / $this->size));//总页数
                if($type == 1){
                    $fields = 'id,title,cover,comid,addtime,read_num,content,istop,recommend,from,jread_num,share_top,share_buttom';
                }elseif($type == 2){
                    $fields = 'id,title,cover';
                }
                $lists = $model->getlists($map,$this->from, $this->size,$fields);
                foreach($lists as $k=>$v){
                    $http = substr($v['cover'],0,4);
                    if($http != 'http'){
                        $lists[$k]['cover'] = config('domain_name').$v['cover'];
                    }
                    if($type == 1){
                        $lists[$k]['addtime'] = date('Y-m-d',$v['addtime']);
                        $lists[$k]['content'] = percent($v['content']);
                        $lists[$k]['read_num'] = $v['read_num'] + $v['jread_num'];
                    }
                    if(!empty($param['of'])){
                        if($type == 2){
                            $user = $this->user;
                            $map1 = [];
                            $map1['of'] = $param['of'];
                            $map1['user_id'] = $user['id'];
                            $map1['status'] = 0;

                            $cmodel = new ClassesModel;
                            $class = $cmodel->getAllData($map1,'id','id,itemid');
                            $class = array_column($class,'itemid','id');
                            if(in_array($v['id'],$class)){
                                $lists[$k]['has'] = 1;
                            }else{
                                $lists[$k]['has'] = 0;
                            }
                        }
                    }
                    if($param['id'] == 7){
                        $date = explode(',',date('n,d',strtotime($v['addtime'])));
                        $day = $date[1];
                        $montharray = array('一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月');
                        $month = $montharray[$date[0] - 1];
                        $weekarray=array("日","一","二","三","四","五","六");
                        $week = "周".$weekarray[date("w")];
                        $lists[$k]['month'] = $month;
                        $lists[$k]['day'] = $day;
                        $lists[$k]['week'] = $week;
                    }


//                    if(!empty($v['comid'])){
//                        $company = $cmodel->findData($v['comid'],'id','comname');
//                        $lists[$k]['comname'] = $company['comname'];
//                    }
                }
            }elseif ($type == 3){
                $map['states'] = 1;
                $map['itemid'] = $param['id'];
                $count = $vmodel->getCounts($map);//满足条件的总数
                $allpage = intval(ceil($count / $this->size));//总页数
                $lists = $vmodel->getlists($map,$this->from, $this->size);
                foreach($lists as $k=>$v){
                    $http = substr($v['url'],0,4);
                    if($http != 'http'){
                        $lists[$k]['url'] = config('domain_name').$v['url'];
                    }
                    $http = substr($v['img'],0,4);
                    if($http != 'http'){
                        $lists[$k]['img'] = config('domain_name').$v['img'];
                    }
                    $lists[$k]['play_num'] = $v['play_num'] + $v['jplay_num'];
                    $lists[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                }
            }elseif($type == 4 || $type == 5){
                $map['states'] = 1;
                $map['itemid'] = $param['id'];
                $count = $mmodel->getCounts($map);//满足条件的总数
                $allpage = intval(ceil($count / $this->size));//总页数
                $lists = $mmodel->getlists($map,$this->from, $this->size);
                foreach($lists as $k=>$v){
                    $http = substr($v['url'],0,4);
                    if($http != 'http'){
                        $lists[$k]['url'] = config('domain_name').$v['url'];
                    }
                    $http = substr($v['img'],0,4);
                    if($http != 'http'){
                        $lists[$k]['img'] = config('domain_name').$v['img'];
                    }
                }
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getEditnum  修改播放次数]
     * @param   type 1修改音乐播放次数
     * @return json
     * @author [C。]
     */
    public function getEditnum()
    {
        $param = input('param.');
        try{
            if(isset($param['type'])){
                if($param['type'] == 1){
                    $mmodel = new MusicModel;
                    $music = $mmodel->findData($param['id'],'id','play_num');
                    $data = array(
                        'id'=>$param['id'],
                        'play_num'=>$music['play_num'] + 1,
                    );
                    $mmodel->editData($data);
                }
            }else{
                $vmodel = new VideoModel;
                $video = $vmodel->findData($param['id'],'id','play_num');
                $data = array(
                    'id'=>$param['id'],
                    'play_num'=>$video['play_num'] + 1,
                );
                $vmodel->editData($data);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '修改失败'.$e->getMessage());
        }
        return admin_json(1, '修改成功');
    }
}